﻿
using System.Collections.Generic;
using EasyPhysic.Collider;
using EasyPhysic.Job;
using NativeTrees;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;



namespace EasyPhysic.Manager
{
    /// <summary>
    /// 物理组，通过物理组进行对象查询,会在对应的物理组中进行查询
    /// </summary>
    public class PhysicGroup
    {
        
        /// <summary>
        /// 拥有的碰撞体
        /// </summary>
        // private  NativeArray<ColliderBody> ownerColliders=new(128,Allocator.Persistent);
        NativeList<ColliderBody> ownerCollidersList=new NativeList<ColliderBody>();


        private Dictionary<int, ColliderWrapper> _wrappers = new Dictionary<int, ColliderWrapper>();
        public int LastCount = 0;
        /// <summary>
        /// 四叉树
        /// </summary>
        private NativeQuadtree<int> quadtree;

        public const int MaxCount=2048;
        
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="results"></param>
        public void ReadTargetId(ColliderBody data, ref UnsafeParallelHashSet<int> results)
        {
            quadtree.RangeAABBUnique(data.Bound,results);
        }
        public PhysicGroup()
        {
            var bounds = new AABB2D(new float2(-1000,-1000),new float2(1000,1000));
            quadtree=new NativeQuadtree<int>(bounds, Allocator.Persistent);
            ownerCollidersList=new NativeList<ColliderBody>(MaxCount*2,Allocator.Persistent);
            // targetAABBs=new NativeHashMap<int,AABB2D>(500,Allocator.Persistent);
        }
        public void RegisterCollider(ColliderWrapper wrapper)
        { 
            _wrappers.Add(wrapper.SerialId,wrapper);
            // ownerCollidersList.Add(wrapper.ShapeInfo);
          
        }
        public void ReleaseCollider(ColliderWrapper wrapper)
        {
            // 在下一帧释放
            wrapper.Release();
        }
        public void Update(int intervalTimer)
        {
            UpdateQuadtree();
        }
        /// <summary>
        /// 更新四叉树
        /// </summary>
        void UpdateQuadtree()
        {
            ownerCollidersList.Clear();

            foreach (var wrapper in _wrappers.Values)
            {
                if (wrapper.IsValid)
                {
                    ownerCollidersList.Add(wrapper.ShapeInfo);
                }
            }
            // for (int i = 0; i < ownerCollidersList.Length; i++)
            // {
            //     var serialId = ownerCollidersList[i].SerialId;
            //     if (_wrappers.ContainsKey(serialId))
            //     {
            //         if (_wrappers[serialId].IsDirty)
            //         {
            //             _wrappers[serialId].IsDirty = false;
            //             ownerCollidersList[i]=_wrappers[serialId].ShapeInfo;
            //         }
            //     }
            //     else
            //     {
            //         ownerCollidersList.RemoveAt(i);
            //     }
            //     
            // }
            var job=new UpdateTreeJob();
            job.quadtree=quadtree;
            job._colliderBodyList=ownerCollidersList;
            quadtree.Clear();
           var handle= job.Schedule();
           handle.Complete();
        }
        public void OnDrawGizmos(Color color)
        {
            foreach (var collider in ownerCollidersList)
            {
                collider.OnDrawGizmos(color);
            }
        }
        public void Clear()
        {
            for (int i = 0; i < ownerCollidersList.Length; i++)
            {
                ownerCollidersList[i] = default;
            }
           
        }
    }
}